package com.csu.mall.product.app;

import com.csu.mall.common.utils.PageUtils;
import com.csu.mall.common.utils.R;
import com.csu.mall.product.entity.CategoryBrandRelationEntity;
import com.csu.mall.product.service.CategoryBrandRelationService;
import com.csu.mall.product.vo.BrandVo;
import com.csu.mall.product.vo.CatelogVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 品牌分类关联
 *
 * @author zhenhuanli
 * @email 743879362@qq.com
 * @date 2022-09-16 11:42:54
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Resource
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @RequestMapping("/catelog/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("data", page.getList());
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
//		categoryBrandRelationService.save(categoryBrandRelation);
        //TODO 校验brandID和catelogId
        categoryBrandRelationService.saveEntity(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 获取分类关联的品牌
     * @param catId
     * @return
     */
    @GetMapping("/brands/list")
    public R getBrandList(@RequestParam Long catId){
        List<BrandVo> list = categoryBrandRelationService.getBrandList(catId);
        return R.ok().put("data", list);
    }

    /**
     * 获取品牌关联的分类
     * @param brandId
     * @return
     */
    @GetMapping("/catelog/list")
    public R getCatelogList(@RequestParam Long brandId){
        List<CatelogVo> list = categoryBrandRelationService.getCatelogList(brandId);
        return R.ok().put("data", list);
    }


}
